﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Controls.Primitives;
using SLLottery.Common;
namespace SLLottery.Controls
{
    public partial class RateGrid : UserControl
    {
        private Popup _tip = new Popup();
        public  bool isFendan = false;
        public int StartNum { get; set; }
        private int RowNumCount=3;
        public int ColNumCount { get; set; }
        public NumPanel numPanel
        {
            get;
            set;
        }
        public TouZhuCtrl touzhu
        {
            get;
            set;
        }
        public List<Dictionary<string,string>> DataSource
        {
            get;
            set;
        }
        public RateGrid()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(RateGrid_Loaded);
        }
        public RateGrid(int startNum,int colCount)
            : this()
        {
            StartNum = startNum;
            ColNumCount = colCount;
        }
        void RateGrid_Loaded(object sender, RoutedEventArgs e)
        {
            createGrid();
        }
        public bool isSSQ = false;
        public void createGrid()
        {
            this.LayoutRoot.Children.Clear();
            Grid grid = this.LayoutRoot;
            grid.Height = RowNumCount * 31;
            grid.Width = 658;// (ColNumCount + 1) * 58;
            for (int i = 0; i < RowNumCount; i++)
            {
                grid.RowDefinitions.Insert(i, new RowDefinition() { Height = new GridLength(31) });
            }
            //for (int i = 0; i < ColNumCount + 1; i++)
            //{
            //    grid.ColumnDefinitions.Insert(i, new ColumnDefinition() { Width = new GridLength(658/(ColNumCount+1)) });
            //}
           for (int j = 0; j < RowNumCount; j++)
            {
                StackPanel sp = new StackPanel();
                sp.Width = 658;
                sp.Orientation = Orientation.Horizontal;
                for (int i = 0; i < ColNumCount + 1; i++) 
                {
               
                    Border border = new Border();
                    border.Width = 658 / (ColNumCount + 1);
                    border.BorderBrush = new SolidColorBrush(MyColor.ConvertColor("#fff4eded"));
                    if (i % 2 == 0)
                    {
                        if (j % 2 == 1)
                            border.BorderThickness = new Thickness(0.5, 0, 0.5, 0);
                        else
                            border.BorderThickness = new Thickness(0.5, 0.5, 0.5, 0.5);
                    }
                    else
                    {
                        if (j % 2 == 1)
                            border.BorderThickness = new Thickness(0, 0, 0, 0);
                        else
                            border.BorderThickness = new Thickness(0, 0.5, 0, 0.5);
                    }
                    if(j==0||i==0)
                        border.Background = Common.MyColor.GetLineBrush();
                    TextBlock tb = new TextBlock();
                    tb.TextAlignment = TextAlignment.Center;
                    tb.Padding = new Thickness(0, 6, 0, 0);
                    tb.Foreground = new SolidColorBrush(Colors.White);
                    
                    border.Child = tb;
                    if (j == 0 || i == 0)
                    { 
                        if (i == 0)
                        {
                            tb.FontSize = 12;
                            switch (j)
                            {
                                case 0:
                                    tb.Text = "号码";
                                    break;
                                case 1:
                                    tb.Text = "赔率";
                                    break;
                                case 2:
                                    tb.Text = "剩余";
                                    break;
                            }
                        }
                        else if (j == 0 && i != 0)
                        {
                            tb.FontSize = 12;
                            if (StartNum <= 9 && isSSQ)
                                tb.Text = "0"+StartNum.ToString();
                            else
                                tb.Text = StartNum.ToString();
                            StartNum = StartNum + 1;
                            tb.MouseEnter += new MouseEventHandler(tb_MouseEnter);
                            tb.MouseLeave += new MouseEventHandler(tb_MouseLeave);
                        }
                        if (i >= 1)
                        {
                            border.MouseLeftButtonDown += (s, e) =>
                                {
                                    if (numPanel == null)
                                        return;
                                    List<Controls.NumButton> listNumButton = numPanel.GetNumButton();
                                    List<Button> listCmdButton = numPanel.GetCmdButton();
                                    foreach (Controls.NumButton nb in listNumButton)
                                    {
                                        Border b =(Border)s;
                                        TextBlock textBlock=(TextBlock)b.Child;
                                        if (nb.tb.Text == textBlock.Text)
                                        {
                                            nb.Click(nb.tb);
                                            string num = numPanel.GetChoosedNum();
                                            if (isSSQ)
                                            {
                                                string[] num_arry = num.Split(',');
                                                touzhu.lbBetCount.Text = num_arry.Length.ToString();
                                            }
                                            else
                                                touzhu.lbBetCount.Text = num.Length.ToString();
                                            break;
                                        }
                                    }
                                };
                        }
                    }
                    else
                    {
                        tb.Text = "封单";
                    }
                    border.Child = tb;
                    sp.Children.Add(border);
                    sp.SetValue(Grid.RowProperty, j);
                //Label1:
                //    border.Child = tb;
                //    sp.Children.Add(border);
                //    sp.SetValue(Grid.ColumnProperty, i);
                //    sp.SetValue(Grid.RowProperty, j);
                //    //border.SetValue(Grid.ColumnProperty, i);
                //    //border.SetValue(Grid.RowProperty, j);
                //    //grid.Children.Add(border);
                //    grid.Children.Add(sp);
                }
                grid.Children.Add(sp);
            }
        }
        public void BindData()
        {
            if (DataSource == null || DataSource.Count <2||MainPage.IsFengDan==true)
            {
                MainPage.TouzhuControl.btBet.IsEnabled = false;
                FengDan();
                return;
            }
            List<string> listHeader = new List<string>();
            foreach(StackPanel sp in LayoutRoot.Children)
            {
                if (sp == LayoutRoot.Children.First())
                {
                    foreach (Border b in sp.Children)
                    {
                        if (b == sp.Children.First())
                            continue;
                        TextBlock tb = (TextBlock)b.Child;
                        listHeader.Add(tb.Text.Trim());
                    }
                    continue;
                }
                for(int i=0;i<sp.Children.Count;i++)
                {
                    Border border = (Border)sp.Children[i];
                    if (i == 0)
                        continue;
                    TextBlock tb = (TextBlock)border.Child;
                    if (sp == LayoutRoot.Children.Last())
                    {
                        if (DataSource[0].ContainsKey(listHeader[i - 1]))
                            tb.Text = Convert.ToDouble(DataSource[1][listHeader[i - 1]]).ToString("F2");
                    }
                    else
                    {
                        if (DataSource[0].ContainsKey(listHeader[i - 1]))
                            tb.Text = Convert.ToDouble(DataSource[0][listHeader[i - 1]]).ToString("F2");
                    }
                }
            }
            isFendan = false;
        }
        public void FengDan()
        {
            MainPage.TouzhuControl.btBet.IsEnabled = false;
            List<string> listHeader = new List<string>();
            foreach (StackPanel sp in LayoutRoot.Children)
            {
                if (sp == LayoutRoot.Children.First())
                {
                    foreach (Border b in sp.Children)
                    {
                        if (b == sp.Children.First())
                            continue;
                        TextBlock tb = (TextBlock)b.Child;
                        listHeader.Add(tb.Text.Trim());
                    }
                    continue;
                }
                for (int i = 0; i < sp.Children.Count; i++)
                {
                    Border border = (Border)sp.Children[i];
                    if (i == 0)
                        continue;
                    TextBlock tb = (TextBlock)border.Child;
                    tb.Text = "封单";
                }
            }
            isFendan = true;
        }
        void tb_MouseLeave(object sender, MouseEventArgs e)
        {
            Border border = (Border)((TextBlock)sender).Parent;
            border.Background = Common.MyColor.GetLineBrush();
            //_tip.IsOpen = false;
        }
        void tb_MouseEnter(object sender, MouseEventArgs e)
        {
            Border border = (Border)((TextBlock)sender).Parent;
            border.Background = new SolidColorBrush(Colors.Orange);

            //Controls.TipTool tip = new TipTool();
            //_tip.Child = tip;
            //_tip.HorizontalOffset = e.GetPosition(null).X+10;
            //_tip.VerticalOffset = e.GetPosition(null).Y+5;
            //_tip.IsOpen = true;
        }
    }
}
